package com.yidu.best.systemManagement.resourceManagement.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.best.systemManagement.resourceManagement.domain.Resource;
import com.yidu.best.systemManagement.resourceManagement.service.ResourceService;

import net.sourceforge.pinyin4j.PinyinHelper;

@Controller
@RequestMapping("/sys/")
public class ResourceController {

	@Autowired
	private	ResourceService ResourceService;
	/**
	 * 跳转页面
	 * @return
	 */
	@RequestMapping("resourceMap")
	public String resourceMap() {
		return "/sys/resource/resource";
	}

	/**
	 * 查找所有资源
	 * @param resource 资源对象
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "resources", method = RequestMethod.GET)
	public Map<String, Object> findAll(Resource resource) {
		Map<String, Object> findAll = ResourceService.findAll(resource);
		return findAll;
	}

	/**
	 * 删除资源
	 * @param resourceId 资源ID
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "resource/{resourceId}", method = RequestMethod.DELETE)
	public int DeleteResource(@PathVariable("resourceId") String resourceId) {
		int deleteResource = 0;
		try {
			String[] resource = resourceId.split(",");
			for (String string : resource) {
				deleteResource = ResourceService.DeleteResource(string);
			}
		} catch (DataAccessException e) {
			System.out.println("不能删除主外键");
		}
		return deleteResource;
	}

	/**
	 * 添加资源
	 * @param resource 资源对象
	 * @return
	 */
	@RequestMapping(value = "resource", method = RequestMethod.POST)
	public String AddResource(Resource resource) {
		ResourceService.AddResource(resource);
		return "redirect:resourceMap";
	}

	/**
	 * 修改资源
	 * @param resource 资源对象
	 * @return
	 */
	@RequestMapping(value = "resource", method = RequestMethod.PUT)
	public String UpdateResource(Resource resource) {
		ResourceService.UpdateResource(resource);
		return "redirect:resourceMap";
	}
	
	/**
	 * 修改状态码
	 * @param resource 资源对象
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "Updateusable" , method = RequestMethod.PUT)
	public int Updateusable(Resource resource){
		int updateusable = ResourceService.Updateusable(resource);
		return updateusable;
	}

	/**
	 * 获取每个汉字的首字母小写
	 * 
	 * @param str
	 * @return
	 */
	@RequestMapping(value = "pinyin/{name}", method = RequestMethod.GET)
	@ResponseBody
	public String getPinYinHeadChar(@PathVariable("name") String str) {
		String convert = "";
		for (int i = 0; i < str.length(); i++) {
			char word = str.charAt(i);
			// 提取汉字的首字母
			String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
			if (pinyinArray != null) {
				convert += pinyinArray[0].charAt(0);
			} else {
				convert += word;
			}
		}
		// 获取小写的单词
		return convert.toLowerCase();
	}
	
	/**
	 * 获取名字
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "rName")
	public List<String> getRName() {
		List<Resource> resources = ResourceService.selectAll();
		List<String> rName = new ArrayList<String>();
		for (Resource resource : resources) {
			rName.add(resource.getResourceName());
		}
		return rName;
	}
	
	/**
	 * 获取父资源Id
	 * @param name
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getPid/{rName}")
	public String getPid(@PathVariable("rName") String name) {
		List<Resource> resources = ResourceService.selectAll();
		String rId = null;
		for (Resource resource : resources) {
			if (resource.getResourceName().equals(name)) {
				rId = resource.getResourceId();
			}
		}
		return rId;
	}
}
